package com.mingqi.enetconnected.view.Dialog.single;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.date.DateTimeInfo;
import com.mingqi.enetconnected.util.resource.ResourceUtils;
import com.mingqi.enetconnected.view.BorderRadioButton;

import java.util.ArrayList;
import java.util.List;

public class GridViewAdapter extends BaseAdapter {

    private List<DateTimeInfo> data;
    private int currentPosition = ListView.INVALID_POSITION;

    public GridViewAdapter(List<DateTimeInfo> data) {
        if (data == null) {
            data = new ArrayList<>();
        }

        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DateTimeInfo dto = (DateTimeInfo) getItem(position);
        ViewHolder holder = null;
        if (convertView == null) {

            convertView = ResourceUtils.getViewFollowParent(R.layout.item_choose_time, parent);
            holder = new ViewHolder();

            holder.textView = convertView.findViewById(R.id.tv_urge);
            holder.radioButton = convertView.findViewById(R.id.choose_value);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (dto.isEnable()) {
            holder.radioButton.setEnabled(true);
            holder.radioButton.setChecked(dto.isChecked());
            holder.textView.setVisibility(View.GONE);
        } else {
            holder.radioButton.setEnabled(false);
            holder.textView.setVisibility(View.VISIBLE);
        }

        holder.radioButton.setText(dto.getValue());

        return convertView;

    }

    class ViewHolder {
        BorderRadioButton radioButton;
        TextView textView;
    }

    public void selectValue(int position) {
        if (currentPosition != ListView.INVALID_POSITION) {
            DateTimeInfo dto = data.get(currentPosition);
            dto.setChecked(false);
        }
        DateTimeInfo dto = data.get(position);
        dto.setChecked(true);
        currentPosition = position;
        notifyDataSetChanged();
    }


    public String getSelectValue() {
        return data.get(currentPosition).getValue();
    }
    public String getSelectKey() {
        return data.get(currentPosition).getKey();
    }
    public int getSelectPosition() {
        return currentPosition;
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }
}
